package com.net.action.stock;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;

import javax.annotation.Resource;

import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;

import com.googlecode.jsonplugin.annotations.JSON;
import com.net.model.Commodity;
import com.net.model.Provider;
import com.net.model.Role;
import com.net.service.CommodityService;
import com.net.service.ProviderService;
import com.opensymphony.xwork2.ActionSupport;
@Component("commodityManagementAction")
@Scope("prototype")
public class CommodityManagementAction extends ActionSupport{
	private Commodity commodity;
	private Provider provider;
	private CommodityService commodityService;
	private ProviderService providerService;
	private List<Commodity> commodity_list;
	private List<Provider> provider_list;
	private boolean success;
	private int totalProperty;
	private int start;
	private int limit;
	private int current_selected_id;
	private int[] selectedProviderList;
	
	public String list(){
		System.out.println(start+":"+limit+"********");
		commodity_list = commodityService.getCommodities(start/limit, limit, null, null);
		totalProperty = commodityService.getCount();
		success = true;
		return SUCCESS;
	}
	
	public String listProviders(){
		System.out.println(start+":"+limit+"********");
		provider_list = providerService.getProviders(start/limit, limit, null, null);
		totalProperty = providerService.getCount();
		success = true;
		return SUCCESS;
	}
	
	public String search(){
		//System.out.println(start+":"+limit+"********"+"user name or id:" + commodity.getCommodity_name());
		String str = "";
		if("".equals(commodity.getCommodity_name())){
			str = "c.id="+commodity.getId();
		}else if(commodity.getId()==0){
			str = "c.commodity_name like '%"+commodity.getCommodity_name()+"%'";
		}else {
			str = "c.commodity_name like '%"+commodity.getCommodity_name()+"%'" + " or c.id="+commodity.getId();
		}
		commodity_list = commodityService.getCommoditiesByCondition(start/limit, limit, str);
		totalProperty = commodityService.getCount();
		success = true;
		return SUCCESS;
	}
	
	public String searchProviders(){
		System.out.println("current_selected_id:"+current_selected_id);
		String str = "";
		str = "c.id="+current_selected_id;
		commodity_list = commodityService.getCommoditiesByCondition(0,commodityService.getCount(), str);//改成loadById有问题
		List<Provider> providers = new ArrayList<Provider>();
		providers.addAll(commodity_list.get(0).getProviders());
		provider_list = new ArrayList<Provider>();
		for(int i = start; i<start+limit&i<providers.size();i++){
			provider_list.add(providers.get(i));
		}
		commodity_list=null;
		totalProperty = commodityService.getCount();
		success = true;
		return SUCCESS;
	}
	
	public String deleteProviders(){
		//System.out.println("current_selected_id:**00**"+current_selected_id+"(9999999900000length:"+selectedProviderList.length);
		for(int i = 0 ; i < selectedProviderList.length; i++){
			commodity = commodityService.loadCommodityById(current_selected_id);
			commodity.getProviders().remove(providerService.loadProviderById(selectedProviderList[i]));
			System.out.println(commodity.getCommodity_name()+"********");
			commodityService.update(commodity);
		}
		commodity = null;
		success = true;
		return SUCCESS;
	}
	
	public String addProviders(){
		String str = "";
		str = "c.id="+current_selected_id;
		System.out.println("current_selected_id 888:"+current_selected_id);
		commodity_list = commodityService.getCommoditiesByCondition(0,commodityService.getCount(), str);
		commodity = commodity_list.get(0);
		System.out.println("user.getName():"+commodity.getCommodity_name());
		provider_list = commodity.getProviders();
		for(int i = 0 ; i < selectedProviderList.length ; i++){
			provider = providerService.loadProviderById((selectedProviderList[i]));
			provider_list.add(provider);
		}
		commodity.setProviders(provider_list);
		commodityService.update(commodity);
		System.out.println(commodity.getId()+":commodity id");
		
		commodity = null;
		commodity_list = null;
		provider_list = null;
		provider = null;
		
		success = true;
		return SUCCESS;
	}
	
	public Commodity getCommodity() {
		return commodity;
	}

	public void setCommodity(Commodity commodity) {
		this.commodity = commodity;
	}
	@JSON(serialize=false)
	public CommodityService getCommodityService() {
		return commodityService;
	}
	@Resource(name = "commodityService")
	public void setCommodityService(CommodityService commodityService) {
		this.commodityService = commodityService;
	}

	public boolean isSuccess() {
		return success;
	}

	public void setSuccess(boolean success) {
		this.success = success;
	}

	public int getTotalProperty() {
		return totalProperty;
	}

	public void setTotalProperty(int totalProperty) {
		this.totalProperty = totalProperty;
	}

	public int getStart() {
		return start;
	}

	public void setStart(int start) {
		this.start = start;
	}

	public int getLimit() {
		return limit;
	}

	public void setLimit(int limit) {
		this.limit = limit;
	}

	public List<Commodity> getCommodity_list() {
		return commodity_list;
	}

	public void setCommodity_list(List<Commodity> commodity_list) {
		this.commodity_list = commodity_list;
	}
	
	public int getCurrent_selected_id() {
		return current_selected_id;
	}

	public void setCurrent_selected_id(int current_selected_id) {
		this.current_selected_id = current_selected_id;
	}

	public List<Provider> getProvider_list() {
		return provider_list;
	}

	public void setProvider_list(List<Provider> provider_list) {
		this.provider_list = provider_list;
	}
	@JSON(serialize=false)
	public ProviderService getProviderService() {
		return providerService;
	}
	@Resource(name = "providerService")
	public void setProviderService(ProviderService providerService) {
		this.providerService = providerService;
	}

	public int[] getSelectedProviderList() {
		return selectedProviderList;
	}

	public void setSelectedProviderList(int[] selectedProviderList) {
		this.selectedProviderList = selectedProviderList;
	}

	public Provider getProvider() {
		return provider;
	}

	public void setProvider(Provider provider) {
		this.provider = provider;
	}

}
